<template>
  <div>
    <el-dialog :title="showTitle" :visible="dialogVisible" @close="handleCloseDialog">
      <el-form ref="form" :model="form" label-width="90px" :rules="rules">
        <el-form-item label="部落名称" prop="name">
          <el-input v-model="form.name" style="width:90%" placeholder="请输入1-10个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" style="width:90%" placeholder="请输入1-10个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" style="width:90%" placeholder="请选择">
            <el-option v-for="item in people" :key="item.id" :value="item.username" :label="item.username" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" style="width:90%" placeholder="请输入10-300个祝福" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="clickSubmit">确定</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetEmployees } from '@/api/employees'
import { reqAddDepartments, reqGetDepartDetail, reqUpdateDepartments } from '@/api/departments'
export default {
  name: 'AddTem',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    depList: {
      type: Array,
      required: true
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkNameRepeat = (rule, value, callback) => {
      let arr = []
      if (this.form.id) {
        if (this.nodeData.name === value) {
          callback()
          return
        }
        arr = this.depList.filter(item => item.pid === this.nodeData.pid)
      } else {
        arr = this.depList.filter(item => item.pid === this.nodeData
          .id)
      }

      const bool = arr.some(item => item.name === value)
      bool ? callback(new Error('已经能有该部门了')) : callback()
    }
    const checkCodeRepeat = (rule, value, callback) => {
      // 判断是否为编辑的条件
      if (this.form.id && value === this.nodeData.code) {
        callback()
        return
      }
      const bool = this.depList.some(item => item.code === value)
      bool ? callback(new Error('已经有该编码了')) : callback()
    }
    return {
      people: [],
      form: {
        name: '',
        introduce: '',
        code: '',
        manager: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '部门名称要求为1-10个字符', trigger: ['blur', 'change'] },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '部门编码要求为1-10个字符', trigger: ['blur', 'change'] },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求为1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getEmployees()
  },
  methods: {
    // 处理关闭窗口
    handleCloseDialog() {
      this.$emit('update:dialogVisible', false)
      this.$refs.form.resetFields()
      this.form.id = null
    },

    async getEmployees() {
      const res = await reqGetEmployees()
      this.people = res.data
    },

    async getDepartDetail(id) {
      const { data } = await reqGetDepartDetail(id)
      this.form = data
      console.log(this.form)
    },
    // 提交
    clickSubmit() {
      this.$refs.form.validate(async flag => {
        if (flag) {
          if (this.form.id) {
            // 进行编辑
            const res = await reqUpdateDepartments(this.form)
            console.log(res)
          } else {
            const res = await reqAddDepartments({ ...this.form, pid: this.nodeData.id })
            console.log(res)
            this.$message.success('新增部门成功')
          }
          this.$emit('add-depts')
          this.handleCloseDialog()
        }
      })
    }
  }
}
</script>

<style>

</style>
